﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using Caliburn.Core.Metadata;
using XWord.GUI.Presenters.Interface;
using System.ComponentModel;
using XWord.GUI.Views.Fundamental;
using System.Collections;
using System.Threading;
using XWord.Fundamental.Domain.Services;
using Caliburn.Core;
using System.Windows.Documents;
using XWord.Common;

namespace XWord.GUI.Presenters.Adorns
{
    [Singleton(typeof(WordAdornerPresenter))]
    public class WordAdornerPresenter : PropertyChangedBase
    {
        public WordAdornerPresenter()
        {
        }

        public Hashtable _examCriterion;
        public Hashtable ExamCriterion
        {
            get
            {
                return _examCriterion;
            }
        }

        public void Initialize()
        {
            
            //ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
            //{
            //    if (_examCriterion == null)
            //    {
            //        IWordService wordSv = RemotingServiceFactory.GetWordService();

            //        _examCriterion = wordSv.GetDataFromBinaryFile(BinaryFiles.ExamCriterion);
            //        NotifyOfPropertyChange("ExamCriterion");
            //    }
            //}));
            WordExplorerPresenter.PropertyChanged += WordExplorerPresenter_PropertyChanged;
        }

        void WordExplorerPresenter_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsViewLoaded")
            {
                WordExplorerPresenter.AdornerElement.Loaded += delegate
                {
                    var adorners = AdornerLayer.GetAdornerLayer(WordExplorerPresenter.AdornerElement).GetAdorners(WordExplorerPresenter.AdornerElement);
                    if (adorners == null)
                        WordExplorerPresenter.AddAdornerToItemsSource(new WordMarkAdorner(WordExplorerPresenter.AdornerElement));
                };
                WordExplorerPresenter.AdornerElement.SizeChanged += delegate {
                    var adorners = AdornerLayer.GetAdornerLayer(WordExplorerPresenter.AdornerElement).GetAdorners(WordExplorerPresenter.AdornerElement);
                    if (adorners == null)
                        WordExplorerPresenter.AddAdornerToItemsSource(new WordMarkAdorner(WordExplorerPresenter.AdornerElement));
                };
                WordExplorerPresenter.PropertyChanged -= WordExplorerPresenter_PropertyChanged;
            }
        }

        public IWordExplorerPresenter WordExplorerPresenter { get; set; }
    }
}
